package com.me.study.app.rvRichEditor.provider

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.text.Editable
import android.text.TextWatcher
import android.view.MotionEvent
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.me.study.R
import com.me.study.app.data.NotifyType
import com.me.study.app.data.RichEditorType
import com.me.study.app.rvRichEditor.listener.EditorTouchListener
import com.me.study.app.rvRichEditor.RichEditorData
import com.me.study.weight.RoundCornerTransform

/**
 *create author : FZW
 *create date  : 2024/9/18
 */
class RichEditorImageProvider(private val listener: EditorTouchListener) :
    BaseItemProvider<RichEditorData>() {

    override val itemViewType = RichEditorType.IMAGE.VALUE

    override val layoutId = R.layout.peovider_rich_editor_image

    @SuppressLint("ClickableViewAccessibility")
    override fun convert(helper: BaseViewHolder, item: RichEditorData) {
        val imageView = helper.getView<ImageView>(R.id.iv_image)
        val viewMove = helper.getView<ImageView>(R.id.iv_move)
        val textWatcher = InputTextWatcher(item)
        val requestOptions = RequestOptions.bitmapTransform(RoundCornerTransform(imageView.scaleType, 5, 5, 0, 0))
        Glide.with(context).load(item.s1).apply(requestOptions).into(imageView)
        val content = item.s2.trim()
        val view = helper.getView<EditText>(R.id.ed_input)
        view.setText(content)
        view.setSelection(content.length)
        view.clearFocus()
        if (view.tag != null) {
            view.removeTextChangedListener(textWatcher)
            view.tag = null
        }
        view.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                view.addTextChangedListener(textWatcher)
                view.tag = textWatcher
            } else {
                view.removeTextChangedListener(textWatcher)
                view.tag = null
            }
        }
        viewMove.setOnTouchListener { p0, p1 ->
            when (p1.action) {
                MotionEvent.ACTION_DOWN -> {
                    listener.onItemDown(helper)
                }
            }
            true
        }

    }

    override fun convert(helper: BaseViewHolder, item: RichEditorData, payloads: List<Any>) {
        for (itemPay in payloads) {
            when (itemPay) {
                NotifyType.START_MOVE.VALUE -> {
                    helper.setGone(R.id.ed_input, true)
                    val imageView = helper.getView<ImageView>(R.id.iv_image)
                    val startHeight = imageView.height // 获取当前高度
                    val endHeight = SizeUtils.dp2px(55f) // 设定新的目标高度
                    val animator = ValueAnimator.ofInt(startHeight, endHeight)
                    animator.duration = 200
                    animator.addUpdateListener { animation -> // 获取动画的当前值（即高度）
                        val image = animation.animatedValue as Int
                        // 更新ImageView的LayoutParams来改变高度
                        val params = imageView.layoutParams
                        params.height = image
                        imageView.layoutParams = params
                    }
                    animator.start()
                }
                NotifyType.END_MOVE.VALUE -> {
                    helper.setGone(R.id.ed_input, false)
                    val imageView = helper.getView<ImageView>(R.id.iv_image)
                    val startHeight = imageView.height // 获取当前高度
                    val endHeight = ScreenUtils.getAppScreenWidth() // 设定新的目标高度
                    val animator = ValueAnimator.ofInt(startHeight, endHeight)
                    animator.duration = 200
                    animator.addUpdateListener { animation -> // 获取动画的当前值（即高度）
//                        val image = animation.animatedValue as Int
                        // 更新ImageView的LayoutParams来改变高度
                        val params = imageView.layoutParams
                        params.height = SizeUtils.dp2px(260f)
                        imageView.layoutParams = params
                    }
                    animator.start()
                }
            }
        }
    }

    inner class InputTextWatcher(private var item: RichEditorData) : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
        override fun afterTextChanged(s: Editable?) {
            if (s.toString().isNotEmpty()) {
                item.s2 = s.toString()
            } else {
                item.s2 = ""
            }
        }
    }

}